package com.example.kotlinview.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlinview.R

class KtRvAdapter(val lists:MutableList<KtClassBean>):RecyclerView.Adapter<KtRvAdapter.ViewHolder>() {
    var mOnItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener){
        mOnItemClickListener = onItemClickListener
    }
/*    ————————————————
    版权声明：本文为CSDN博主「JTLack」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
    原文链接：https://blog.csdn.net/jtlack/article/details/109540424*/

    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        var tv: TextView =view.findViewById(R.id.tv_main_item);

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
       val view= LayoutInflater.from(parent.context)
            .inflate(R.layout.layout_main_item,parent,false)
        return ViewHolder(view);
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
          val classBean=lists[position];
          holder.tv.text=classBean.name
          holder.tv.setOnClickListener {
              mOnItemClickListener?.onItemClick(position)
          }

    }

    override fun getItemCount(): Int {
        return lists?.size
    }
    // 定义一个接口，用接口的形式来处理点击事件
    interface OnItemClickListener {
        fun onItemClick(pos: Int)
    }
}